 //
//  MListService.m
//  iOS-LoginUIController
//
//  Created by Apple-19 on 15/10/24.
//  Copyright (c) 2015年 Apple-19. All rights reserved.
//

#import "MListService.h"
#define PRODUCT_LIST_URL [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/product/searchproduct"]


@implementation MListService
+(id)sharedClient
{
    static dispatch_once_t pred;
    static MListService *shared_instance=nil;
    dispatch_once(&pred, ^{
        shared_instance=[[self alloc] init];
        shared_instance.client=[self sharedHTTPClient];
    });
    return  shared_instance;
}
-(void)getProductListWithKid:(NSString *)kid latitude:(NSString *)latitude longtitude:(NSString *)longtitude radius:(NSString *)radius sort:(NSString *)sort typeId:(NSString *)typeID priceId:(NSString *)priceID sizeId:(NSString *)sizeID pageIndex:(NSString *)pageIndex completion:(getProductListCompletionHandler)success failure:(getProductErrorHandler)failure
{
    //    定义可变数组存放数据
    NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
    if (kid) {
        [params setObject:kid forKey:@"kid"];
    }
    if (latitude) {
        [params setObject:latitude forKey:@"lat"];
    }
    if (longtitude) {
        [params setObject:longtitude forKey:@"lng"];
    }
    if (radius) {
        [params setObject:radius forKey:@"radius"];
    }
    if (sort) {
        [params setObject:sort forKey:@"sort"];
    }
    if (typeID) {
        [params setObject:typeID forKey:@"typeid"];
    }
    if (priceID) {
        [params setObject:priceID forKey:@"priceid"];
    }
    if (sizeID) {
        [params setObject:sizeID forKey:@"sizeid"];
    }
    if (pageIndex) {
        [params setObject:pageIndex forKey:@"pageindex"];
    }else{
        [params setObject:@"0" forKey:@"pageindex"];
    }
    NSString *path=PRODUCT_LIST_URL;
    if (params.count>0) {
        path=[NSString stringWithFormat:@"%@?",path];
        for (id key in params) {
            path=[NSString stringWithFormat:@"%@%@=%@&",path,key,params[key]];
        }
    }
    //    请求
    [self.client postPath:path parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject){
        id JSON=((AFJSONRequestOperation *)operation).responseJSON;
        if (!JSON) {
            failure(@"错误");
            return ;
        }
        NSArray *data=JSON[@"contents"];
        success([[[MListHttpResult alloc]init] getListResultWithArrayJSON:data]);
    } failure:^(AFHTTPRequestOperation *operation,NSError *error){
        failure(@"网络异常");
    }];
}
@end
